package com.card.hwatt.assembler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;

import com.card.hwatt.core.ObjectUtil;
import com.card.hwatt.entity.*;

/**
 * 班次
 * @author Administrator
 *
 */
public final class ScheduleAssembler {

	private static Logger logger = Logger.getLogger(ScheduleAssembler.class);

	public static List<com.card.hwatt.dto.Schedule> convert(List<Schedule> list) {
		List<com.card.hwatt.dto.Schedule> rows = new ArrayList<com.card.hwatt.dto.Schedule>();
		for (Schedule item : list) {
			com.card.hwatt.dto.Schedule dto = convert(item);
			rows.add(dto);
		}
		return rows;
	}

	public static com.card.hwatt.dto.Schedule convert(Schedule entity) {

		com.card.hwatt.dto.Schedule dto = new com.card.hwatt.dto.Schedule();
		ObjectUtil.fieldCopy(entity, dto);

		if (entity.department != null) {
			dto.departmentId = entity.department.id;
		}

		if (entity.morning != null) {
			dto.morningId = entity.morning.id;
			dto.morningName = entity.morning.name;
			dto.morningStartTime = entity.morning.startTime;
			dto.morningEndTime = entity.morning.endTime;
			dto.morningBeforeStart = entity.morning.beforeStart;
			dto.morningBeforeEnd = entity.morning.beforeEnd;
			dto.morningAfterStart = entity.morning.afterStart;
			dto.morningAfterEnd = entity.morning.afterEnd;

			dto.morning = dto.morningStartTime.substring(0, 2) + ":"
					+ dto.morningStartTime.substring(2, 4);

			Calendar calendar = Calendar.getInstance();
			calendar.set(2000, 0, 1,
					Integer.valueOf(dto.morningStartTime.substring(0, 2)),
					Integer.valueOf(dto.morningStartTime.substring(2, 4)), 0);
			calendar.add(Calendar.SECOND, dto.morningEndTime * 60);

			dto.morning += "-" + DateFormatUtils.format(calendar, "HH:mm");
		}

		if (entity.afternoon != null) {
			dto.afternoonId = entity.afternoon.id;
			dto.afternoonName = entity.afternoon.name;
			dto.afternoonStartTime = entity.afternoon.startTime;
			dto.afternoonEndTime = entity.afternoon.endTime;
			dto.afternoonBeforeStart = entity.afternoon.beforeStart;
			dto.afternoonBeforeEnd = entity.afternoon.beforeEnd;
			dto.afternoonAfterStart = entity.afternoon.afterStart;
			dto.afternoonAfterEnd = entity.afternoon.afterEnd;

			dto.afternoon = dto.afternoonStartTime.substring(0, 2) + ":"
					+ dto.afternoonStartTime.substring(2, 4);

			Calendar calendar = Calendar.getInstance();
			calendar.set(2000, 0, 1,
					Integer.valueOf(dto.afternoonStartTime.substring(0, 2)),
					Integer.valueOf(dto.afternoonStartTime.substring(2, 4)), 0);
			calendar.add(Calendar.SECOND, dto.afternoonEndTime * 60);

			dto.afternoon += "-" + DateFormatUtils.format(calendar, "HH:mm");
		}

		if (entity.night != null) {
			dto.nightId = entity.night.id;
			dto.nightName = entity.night.name;
			dto.nightStartTime = entity.night.startTime;
			dto.nightEndTime = entity.night.endTime;
			dto.nightBeforeStart = entity.night.beforeStart;
			dto.nightBeforeEnd = entity.night.beforeEnd;
			dto.nightAfterStart = entity.night.afterStart;
			dto.nightAfterEnd = entity.night.afterEnd;

			dto.night = dto.nightStartTime.substring(0, 2) + ":"
					+ dto.nightStartTime.substring(2, 4);

			Calendar calendar = Calendar.getInstance();
			calendar.set(2000, 0, 1,
					Integer.valueOf(dto.nightStartTime.substring(0, 2)),
					Integer.valueOf(dto.nightStartTime.substring(2, 4)), 0);
			calendar.add(Calendar.SECOND, dto.nightEndTime * 60);

			dto.night += "-" + DateFormatUtils.format(calendar, "HH:mm");

			if (CollectionUtils.isNotEmpty(entity.nightCheckList)) {

				Collections.sort(entity.nightCheckList,
						new Comparator<NightCheck>() {

							@Override
							public int compare(NightCheck o1, NightCheck o2) {
								return Integer.valueOf(o1.orderBy).compareTo(
										Integer.valueOf(o2.orderBy));
							}
						});

				dto.nightCheckList = new com.card.hwatt.dto.NightCheck[entity.nightCheckList
						.size()];
				for (int i = 0; i < entity.nightCheckList.size(); i++) {
					NightCheck item = entity.nightCheckList.get(i);
					dto.nightCheckList[i] = new com.card.hwatt.dto.NightCheck();

					if (item.startTime != null && item.endTime != null) {

						calendar.set(
								2000,
								0,
								1,
								Integer.valueOf(item.startTime.substring(0, 2)),
								Integer.valueOf(item.startTime.substring(2, 4)),
								0);
						dto.nightCheckList[i].startTime = DateFormatUtils
								.format(calendar, "HH:mm");
						calendar.add(Calendar.SECOND, item.endTime * 60);

						dto.nightCheckList[i].endTime = DateFormatUtils.format(
								calendar, "HH:mm");
					}
				}
			}

		}

		return dto;
	}

	public static Schedule convert(com.card.hwatt.dto.Schedule dto) {

		Schedule entity = new Schedule();
		ObjectUtil.fieldCopy(dto, entity);

		if (dto.morningId != null) {
			entity.morning = new WorkTime();
			entity.morning.id = dto.morningId;
		}

		if (dto.afternoonId != null) {
			entity.afternoon = new WorkTime();
			entity.afternoon.id = dto.afternoonId;
		}

		if (dto.nightId != null) {
			entity.night = new WorkTime();
			entity.night.id = dto.nightId;

			if (ArrayUtils.isNotEmpty(dto.nightCheckList)) {

				Arrays.sort(dto.nightCheckList,
						new Comparator<com.card.hwatt.dto.NightCheck>() {
							@Override
							public int compare(
									com.card.hwatt.dto.NightCheck o1,
									com.card.hwatt.dto.NightCheck o2) {
								return Integer.valueOf(o1.orderBy).compareTo(
										Integer.valueOf(o2.orderBy));
							}
						});

				entity.nightCheckList = new ArrayList<NightCheck>();
				for (com.card.hwatt.dto.NightCheck item : dto.nightCheckList) {
					NightCheck check = new NightCheck();
					check.id = UUID.randomUUID();
					check.schedule = entity;
					try {
						if (StringUtils.isNotEmpty(item.startTime)
								&& StringUtils.isNotEmpty(item.endTime)) {
							Calendar calendar = Calendar.getInstance();
							int start = Integer.valueOf(item.startTime
									.split(":")[0]);
							calendar.set(2000, 1, 1, start, Integer
									.valueOf(item.startTime.split(":")[1]), 0);
							String pattern = "HHmm";
							check.startTime = DateFormatUtils.format(calendar,
									pattern);

							Calendar endTime = Calendar.getInstance();
							int end = Integer
									.valueOf(item.endTime.split(":")[0]);
							endTime.set(
									2000,
									1,
									1 + (start < end ? 0 : 1),
									end,
									Integer.valueOf(item.endTime.split(":")[1]),
									0);

							check.endTime = Math.abs(millisecondsToDays(
									endTime, calendar));
						}
					} catch (Exception e) {
						logger.error(e);
					}
					check.orderBy = entity.nightCheckList.size();
					entity.nightCheckList.add(check);
				}
			}
		}

		if (dto.departmentId != null) {
			entity.department = new Department();
			entity.department.id = dto.departmentId;
		}

		return entity;
	}

	private static int millisecondsToDays(Calendar endTime, Calendar calendar) {
		double todayMs = Math.round(Double
				.valueOf(calendar.getTime().getTime()) / 1000D);
		double returnMs = Math.round(Double
				.valueOf(endTime.getTime().getTime()) / 1000D);
		double intervalMs = todayMs - returnMs;
		return Double.valueOf(Math.round(intervalMs / 60D)).intValue();
	}
}
